import { BlockType, LinkMark, MyEvent } from "./Constants";
import Line from "./features/Line";
import { Block, GridSystem, MyArrow } from "./Index";
import IPoint from "./interface/IPoint";
import { LinkStyle } from "./Constants"

class Link extends Line {

    blocks: Block[] = [];  // 关联的元素, 目前只有两个点,起点和终点

    constructor(start: Block | IPoint, end: Block | IPoint, connectStyle?: LinkStyle) {
        super();
        this.class = BlockType.LINK;
        if (connectStyle) { this.connectStyle = connectStyle }
        if (start instanceof Block && end instanceof Block) {
            start.isLinkMark = end.isLinkMark = true;
            this.blocks.push(start);
            this.blocks.push(end);
        } else {
            this.addPoint(start)
            this.addPoint(end)
        }
        GridSystem.addLink(this);
        document.addEventListener(MyEvent.DRAW_LINK, (e: any) => {
            let { x, y, block } = e.detail;
            let index = this.blocks.findIndex(Lblock => Lblock.id == block.id);   // 第一个是起始点,第二个是目标点
            if (index != undefined) {
                if (index == 0) {
                    this.points[0] = { x, y }
                }
                if (index == 1) {
                    this.points[1] = { x, y }
                }
            }
        })
    }
}

export default Link;